package com.ruihang.generalibrary.ui.util;

import android.support.annotation.NonNull;
import android.support.v4.util.ArrayMap;

import com.jiongbull.jlog.JLog;

import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2017/7/7.
 */

public class LittleLifeManager {
    private ArrayMap<String, WeakReference<IAndroidLittleLife>> members;

    private Object syncObj;

    public LittleLifeManager() {
        members = new ArrayMap<>();
        syncObj=new Object();
    }

    public void onResume() {
        JLog.e("nnan  m size="+members.size());
        for (int i = 0; i < members.size(); i++) {
            IAndroidLittleLife ia = getLifeInf(i);
            if (ia != null) {
                ia.onIResume();
            }
        }
    }

    public void onPause() {
        for (int i = 0; i < members.size(); i++) {
            IAndroidLittleLife ia = getLifeInf(i);
            if (ia != null) {
                ia.onIPause();
            }
        }
    }

    public void onDestory() {
        for (int i = 0; i < members.size(); i++) {
            IAndroidLittleLife ia = getLifeInf(i);
            if (ia != null) {
                ia.onIDestory();
            }
        }
        members.clear();
    }

    public <T extends IAndroidLittleLife> void addLifeInf(T t){
        add(t,true);
    }

    public <T extends IAndroidLittleLife> void addLifeInf(T t,boolean callOnResume){
        add(t,callOnResume);
    }

    public <T extends IAndroidLittleLife> void removeLifeInf(T t,boolean callDestory){
        remove(t,callDestory);
    }

    public <T extends IAndroidLittleLife> void removeLifeInf(T t){
        remove(t,true);
    }

    private <T extends IAndroidLittleLife> void add(T t,boolean callResume){
        synchronized (syncObj){
            if (t!=null) {
                String key=getStringName(t);
                int pos=members.indexOfKey(key);
                if (pos>=0){
                    WeakReference<IAndroidLittleLife> wia=members.valueAt(pos);
                    if (wia==null){
                        members.setValueAt(pos,new WeakReference<IAndroidLittleLife>(t));
                    }else {
                        if (wia.get()==null){
                            members.setValueAt(pos,new WeakReference<IAndroidLittleLife>(t));
                        }
                    }
                }else {
                    add(key,t,callResume);
                }
            }
        }
    }

    private <T extends IAndroidLittleLife> void add(String key,T t,boolean callResume){
        if (callResume){
            t.onIResume();
        }
        members.put(key,new WeakReference<IAndroidLittleLife>(t));
    }

    private <T extends IAndroidLittleLife> void remove(T t,boolean callDestory){
        synchronized (syncObj) {
            if (t==null){
                return;
            }
            String key=getStringName(t);
            if (members.containsKey(key)) {
                if (callDestory) {
                    t.onIDestory();
                }
                members.remove(key);
            }
        }
    }

    private void remove(String key){
        synchronized (syncObj){
            members.remove(key);
        }
    }

    private String getStringName(@NonNull Object obj){
        return obj.getClass().getName()+"-"+Integer.toHexString(obj.hashCode());
    }

    private IAndroidLittleLife getLifeInf(int pos) {
        WeakReference<IAndroidLittleLife> wi = members.valueAt(pos);
//        JLog.e("nnan pos="+pos+"   "+wi);
        if (wi != null) {
            return wi.get();
        }else {
            members.removeAt(pos);
        }
        return null;
    }

}
